package com.a.ims.controller;

import com.a.ims.entity.CardSelect;
import com.a.ims.entity.Cardmate;
import com.a.ims.entity.User;
import com.a.ims.service.AdminlogService;
import com.a.ims.util.R;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.a.ims.entity.Card;
import com.a.ims.service.CardService;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    @Autowired
    private AdminlogService adminlogService;

    //检测账户
    private <T> R<T> checkUser(HttpSession session) {
        //检测权限
        User user = (User) session.getAttribute("user");
        //检测登录
        if(user == null) {
            System.out.println("用户未登录！");
            return R.error("请先登录！");
        }
        //检测权限
        if(user.getPermission() < 1) {
            System.out.println("用户权限不足！");
            return R.error("权限不足！");
        }
        return null;
    }
    //记录日志
    private void log(HttpSession session, String log) {
        User user = (User) session.getAttribute("user");
        adminlogService.log(user.getAdminId(), log);
    }

    @RequestMapping("/selectCard")
    public R<List<Card>> selectCard(@RequestBody CardSelect cardSelect, HttpSession session) {
        System.out.println("搜索卡片信息: " +cardSelect);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        if(cardSelect.getMinBuyTime()!= null)
            System.out.println(formatter.format(cardSelect.getMinBuyTime()));
        //检测用户权限
        R<List<Card>> checkResult = checkUser(session);
        if(checkResult != null) {
            return checkResult;
        }
        //查询卡片信息
        List<Card> cards = cardService.selectCard(cardSelect);
        if(cards == null){
            System.out.println("查询失败！");
            return R.error("查询失败！");
        }
        System.out.println("查询成功："+cards);
        return R.ok(cards);
    }

    @RequestMapping("/insertCard")
    public R<Integer> insertCard(@RequestBody Card card, HttpSession session) {
        System.out.println("插入卡片信息: " +card);
        //检测用户权限
        R<Integer> checkResult = checkUser(session);
        if(checkResult != null) {
            return checkResult;
        }
        //插入卡片信息
        int result = cardService.insertCard(card);
        if(result == 0){
            System.out.println("插入失败！");
            return R.error("插入失败！");
        }
        System.out.println("插入成功！");
        //记录日志
        log(session, "插入卡片信息: " +card);
        return R.ok(result);
    }

    @RequestMapping("/deleteCard")
    public R<Integer> deleteCard(@RequestBody Card card , HttpSession session) {
        System.out.println("删除卡片信息: " +card);
        //检查用户权限
        R<Integer> checkResult = checkUser(session);
        if(checkResult != null) {
            return checkResult;
        }
        //删除卡片信息
        int result = cardService.deleteCard(card.getCardId());
        if(result == 0){
            System.out.println("删除失败！");
            return R.error("删除失败！");
        }
        System.out.println("删除成功！");
        //记录日志
        log(session, "删除卡片信息: " +card);
        return R.ok(result);
    }

    @RequestMapping("/toggleSaleStatus")
    public R<Integer> toggleSaleStatus(@RequestBody Card card, HttpSession session) {
        System.out.println("更新卡片信息: " +card);
        //检测用户权限
        R<Integer> checkResult = checkUser(session);
        if(checkResult != null) {
            return checkResult;
        }
        //更新卡片信息
        int result = cardService.toggleSaleStatus(card);
        if(result == 0){
            System.out.println("更新失败！");
            return R.error("更新失败！");
        }
        System.out.println("更新成功！");
        //记录日志
        log(session, "更新卡片信息: " +card);
        return R.ok(result);
    }
    @RequestMapping("/selectCardmate")
    public R<List<Cardmate>> selectCardmate( Cardmate cardmate, HttpSession session) {
        System.out.println("查询卡片信息: " +cardmate);
        //检测用户权限
        R<List<Cardmate>> checkResult = checkUser(session);
        if(checkResult != null) {
            return checkResult;
        }
        //查询卡片信息
        List<Cardmate> cardmates = cardService.selectCardmate(cardmate);
        if(cardmates == null){
            System.out.println("查询失败！");
            return R.error("查询失败！");
        }
        System.out.println("查询成功:"+cardmates);
        return R.ok(cardmates);
    }

    @RequestMapping("/selectWarehouseName")
    public R<List<String>> selectWarehouseName(String warehouseName, HttpSession session) {
        System.out.println("查询仓库名称: " +warehouseName);
        //检测用户权限
        R<List<String>> checkResult = checkUser(session);
        if(checkResult != null) {
            return checkResult;
        }
        //查询仓库名称
        List<String> warehouseNames = cardService.selectWarehouseName(warehouseName);
        if(warehouseNames == null){
            System.out.println("查询失败！");
            return R.error("查询失败！");
        }
        System.out.println("查询成功！");
        return R.ok(warehouseNames);
    }
}
